<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="维修员名称" prop="fixmanName">
        <el-input
          v-model="queryParams.fixmanName"
          placeholder="请输入维修员名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="维修员电话" prop="fixmanPhone">
        <el-input
          v-model="queryParams.fixmanPhone"
          placeholder="请输入维修员电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['rcgl:fixman:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['rcgl:fixman:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['rcgl:fixman:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['rcgl:fixman:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fixmanList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="fixmanId" />
      <el-table-column label="维修员名称" align="center" prop="fixmanName" />
      <el-table-column label="维修员电话" align="center" prop="fixmanPhone" />
      <el-table-column label="维修范围" align="center" prop="fixmanRange" />

      <!-- 维护小区 -->
      <el-table-column label="维护小区" align="center" prop="communities[0].communityName" >
        <template slot-scope="scope">
          <el-tag v-for="item in scope.row.communities" :key="item.communityId" type="primary" style="margin-right: 10px">{{ item.communityName }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="维修员性别" align="center" prop="fixmanSex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.fixmanSex"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['rcgl:fixman:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['rcgl:fixman:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"/>

    <!-- 添加或修改维修员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="维修员名称" prop="fixmanName">
          <el-input v-model="form.fixmanName" placeholder="请输入维修员名称" />
        </el-form-item>
        <el-form-item label="维修员电话" prop="fixmanPhone">
          <el-input v-model="form.fixmanPhone" placeholder="请输入维修员电话" />
        </el-form-item>
        <el-form-item label="维修范围" prop="fixmanRange">
          <el-input v-model="form.fixmanRange" placeholder="请输入维修范围" />
        </el-form-item>


        <!-- 维护小区 下拉框选择 如果是新增则显示所有的小区 如果是修改则显示已维护的小区 
         我现在已经获取了所有的小区信息并存入了communities中，如何将它展示在下拉框中 --> 
         <!--  -->
         <el-form-item label="维护小区" prop="fixmanCommunityIds" >
          <el-select v-model="form.fixmanCommunityIds" multiple placeholder="请选择维护小区">
            <el-option
              v-for="item in communities"
              :key="item.communityId"
              :label="item.communityName"
              :value="item.communityId"
            ></el-option>
          </el-select>
         </el-form-item>
        
        <el-form-item label="维修员性别" prop="fixmanSex">
          <el-select v-model="form.fixmanSex" placeholder="请选择维修员性别">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFixman, getFixman, delFixman, addFixman, updateFixman } from "@/api/rcgl/fixman";
import {listCommunity, getCommunity, delCommunity, addCommunity, updateCommunity} from "@/api/basic/community";
export default {
  name: "Fixman",
  dicts: ['sys_user_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 维修员表格数据
      fixmanList: [],
      //小区集合
      communities:[],
      //
      fixmanCommunityIds:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fixmanName: null,
        fixmanPhone: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  //这里this.getList是调用了listFixman，然后把返回的数据赋值给this.fixmanList，
  created() {
    this.getList();
    // this.getCommunities();
  },
  methods: {
    /** 查询维修员列表 */
    getList() {
      this.loading = true;
      listFixman(this.queryParams).then(response => {
        this.fixmanList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fixmanId: null,
        fixmanName: null,
        fixmanPhone: null,
        fixmanRange: null,
        fixmanSex: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.fixmanId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      //点击新增的时候获取所有的小区信息
      this.getCommunities();
      this.open = true;
      this.title = "添加维修员";
    },

    // 获取小区集合 调用查询所有小区的接口 获得所有小区
    getCommunities(){
      this.loading = true;
      listCommunity().then(response => {
        this.communities = response.rows;
        this.loading = false;
      });
    },
    

    /** 修改按钮操作 */
    handleUpdate(row) {
      // console.log(row)
      this.reset();
      // const fixmanId = row.fixmanId || this.ids
      // getFixman(fixmanId).then(response => {
      //   this.form = response.data;
      this.form = row;
       // 将已维护的小区 ID 设置到 form.fixmanCommunityIds 中
      this.form.fixmanCommunityIds = row.communities.map(item => item.communityId);
      this.getCommunities();
      this.open = true;
      this.title = "修改维修员";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fixmanId != null) {
            updateFixman(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFixman(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fixmanIds = row.fixmanId || this.ids;
      this.$modal.confirm('是否确认删除维修员编号为"' + fixmanIds + '"的数据项？').then(function() {
        return delFixman(fixmanIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('rcgl/fixman/export', {
        ...this.queryParams
      }, `fixman_${new Date().getTime()}.xlsx`)
    }
  },
  
};


</script>
